package com.example.demo.component;

import com.example.demo.config.AppConfig;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

@Configuration
public class GetHttpSession extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        HttpSession httpSession = (HttpSession) request.getHttpSession();

        //  ServerEndpointConfig 继承 EndpointConfig，可以使用子类对象存储 HttpSession，父类就可以获取到 HttpSession
        sec.getUserProperties().put(AppConfig.WEBSOCKET_SESSION_KEY,httpSession);
    }
}
